import { _decorator, Component, director, instantiate, Label, macro, Node, Prefab, random, randomRange, randomRangeInt, resources, tween, v3, Vec3 } from 'cc';
import EventMgr from '../../../../framework/event/EventMgr';
import { EventDefine } from '../../../constant/EventDefine';
import AssetMgr from '../../../../framework/asset/AssetMgr';
import { Hero } from '../../hero/Hero';
import GlobalEventMgr from '../../../common/GlobalEventMgr';
const { ccclass, property } = _decorator;

@ccclass('ChapterBattle')
export class ChapterBattle extends Component {
    @property(Node)
    public startTimeLable: Node;

    @property(Node)
    public enimyCacheNode: Node;

    private isStart = false;
    private startTime = 3;

    @property({type:[Node]})
    public enimyBornPos:Node[] = []
    private gameTime = 0;
    private lastBornTime = 0;

    @property(Hero)
    hero:Hero;

    protected onLoad(): void {
        //注册事件：
        //开始事件
        GlobalEventMgr.Ins.on(EventDefine.GameStart, this.startGame, this);
        //抽卡事件
        GlobalEventMgr.Ins.on(EventDefine.RandomCard, this.randomCards, this);
        //选技能事件
        //进度事件
        GlobalEventMgr.Ins.on(EventDefine.GameStage, this.gameStage, this);
        //暂停事件
        //结束事件
    }
    start() {
        this.startTimeLable.getComponent(Label).string = this.startTime + '';
        //倒计时6秒后出怪
        tween()
            .target(this.startTimeLable)
            .to(0.9, { scale: new Vec3(1, 1, 1) }, { easing: "elasticInOut" })
            .call(() => {
                this.startTime--
                this.startTimeLable.scale = new Vec3(0, 0, 1)
                this.startTimeLable.getComponent(Label).string = this.startTime + '';
                if (this.startTime == -1) {
                    this.startTime = 3;
                    GlobalEventMgr.Ins.emmit(EventDefine.GameStart);
                }
            })
            .union().repeat(4).start()
    }
    startGame() {
        console.log("开始游戏");
        this.isStart = true;
        this.resetData();
        this.enityInit();
    }
    /**
     * 重置战斗场景的数据
     */
    resetData() {
        //人口
        //钱
        //时间
        //速度等
    }
    enityInit() {
        //每隔1秒检查出怪
    }

    randomCards() {
        if (!this.isStart) return;
        console.log("随机卡牌");
    }

    gameStage() {
        if (!this.isStart) return;
        console.log("游戏阶段");
    }

    update(deltaTime: number) {
        if (!this.isStart) return;
        //处理怪物出生
        this.gameTime += deltaTime;
        if (this.lastBornTime + 1 < this.gameTime) {
            console.log("出怪");
            this.lastBornTime = this.gameTime;
            let bornIndex = randomRangeInt(0, this.enimyBornPos.length)
            let pos = this.enimyBornPos[bornIndex].getPosition();
            resources.load("prefabs/enimy/enimy",(e,d)=>{
                let  enimy = instantiate(d as Prefab)
                enimy.position = pos;
                enimy.parent = this.enimyCacheNode;
            })
        }
        //英雄释放技能  
        this.hero.releaseSkill();
    }
}




